package part1.solution7;

/**
 * @Author: shaochong
 * @Date: 2021/8/7
 * @Description:
 */
public class Solution7 {
    public static int reverse(int x) {
        String xx = x + "";
        boolean isFushu = false;
        if (xx.startsWith("-")) {
            xx = xx.substring(1, xx.length());
            isFushu = true;
        }
        xx = new StringBuilder(xx).reverse().toString();

        if (isFushu) {
            xx = "-" + xx;
        }

        int ans = 0;
        try {
            ans = Integer.parseInt(xx);
        } catch(Exception e) {
            ans = 0;
        }
        return ans;
    }

    public static int reverse2(int x) {
        int ans = 0;
        while(x != 0) {
            int t = x % 10;
            if (ans > Integer.MAX_VALUE / 10 || (ans == Integer.MAX_VALUE / 10 && t > 7)) {
                return 0;
            }
            if (ans < Integer.MIN_VALUE / 10 || (ans == Integer.MIN_VALUE / 10 && t < -8)) {
                return 0;
            }
            ans = ans * 10 + t;
            x = x / 10;
        }
        return ans;
    }

        public static void main(String[] args) {
        int a = 1230;
        System.out.println(reverse2(a));
    }
}
